<script>
	import {
		storage
	} from "@/common/storage.js";
	import {
		apis
	} from "./api/api";
	import silenceUpdate from '@/uni_modules/rt-uni-update/js_sdk/silence-update.js' //引入静默更新
	export default {
		onLaunch: function() {
			console.log("App Launch====");
			setTimeout(() => {
				// console.log("loadNativeFont")

				var jNModule = uni.requireNativePlugin("JNModule")
				// console.log("jNModule  == " + jNModule)

				var fonts = ['NotoSerifSC-Bold.otf', 'NotoSerifSC-Medium.otf',
					'NotoSerifSC-Regular.otf', 'NotoSerifSC-SemiBold.otf',
					'RedHatDisplay-Medium.ttf', 'RedHatDisplay-Regular.ttf'
				]

				for (let i = 0; i < fonts.length; i++) {
					let font = fonts[i]
					let fontName = font
					let nameIndex = font.lastIndexOf(".")
					if (nameIndex > 0) {
						fontName = font.substring(0, nameIndex)
					}
					// console.log("font === " + font + " fontName === " + fontName)

					let url = "https://jk.railshj.com"
					if (jNModule != null) {
						let ret = jNModule.getFontFilePath({
							'name': font
						})
						// console.log("loadNativeFont ret  == " + JSON.stringify(ret))
						url = ret.path

						// console.log('loadNativeFont', url)
						uni.loadFontFace({
							global: true,
							family: fontName,
							source: `url("${url}")`,
							success() {
								// console.log('loadNativeFont success ' + font)
							},
							fail(e) {
								console.log('loadNativeFont fail ' + font + ' e == ' + JSON.stringify(
									e))
							}
						})
					}
				}

			}, 800)
			//#ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary');
			plus.key.addEventListener('backbutton', () => {
				const pages = getCurrentPages()
				console.log("pages backbutton ")
				const page = pages[pages.length - 2]
				if (!page) {
					plus.runtime.quit();
				}
			});
			//#endif
		},
		onShow: function(options) {
			//#ifdef APP-PLUS
			// 原生传递参数
			console.log("App Show====" + JSON.stringify(options.referrerInfo));
			// let obj = options.referrerInfo.extraData;
			let obj = {
				"token": "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjMyZjkyOWM3LTY0NGYtNGRjZS04MjdjLWZhMDIyNDEwMGFkNSJ9.GdnyvHrNJjUC5VrsBvu06myn_78PGUf2u7aETelQRIQNXcdClOAgylyE1oGkrQAv_jS8y1zVKGrBECFsezqMlw",
				"userInfo": "{\"admin\":false,\"avatar\":\"http://222.44.91.49:8080/oss-pntest/party-building/image/20230920/11b77a5458a6472c9edfdf36e387e19e.jpeg\",\"delFlag\":\"0\",\"dept\":{\"ancestors\":\"0,199189,198935,198614\",\"children\":[],\"delFlag\":\"0\",\"deptId\":198625,\"deptName\":\"上海维保部\",\"params\":{},\"parentId\":198614,\"status\":\"0\"},\"deptId\":198625,\"email\":\"\",\"employeeId\":\"xwysh01\",\"kingdeeToken\":\"eyJhbGciOiJIUzUxMiJ9.eyJlbXBsb3llZUlkIjoieHd5c2gwMSJ9.SFVPqJGLHIUbYRdijX5bmTMPPFbCHyFw50_du07qXqluiiOwFZoWzKRGsBimUuJcxHg2KzpmvQBJbtyOFd8PDQ\",\"nickName\":\"肖文瑶上海\",\"params\":{},\"phonenumber\":\"18000000000\",\"roles\":[{\"admin\":false,\"createTime\":\"2023-09-28 09:23:18\",\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"flag\":false,\"menuCheckStrictly\":false,\"params\":{},\"roleKey\":\"pbserver_itinerary_manage\",\"roleName\":\"行程管理人员-app\"},{\"admin\":false,\"createTime\":\"2023-09-28 10:02:35\",\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"flag\":false,\"menuCheckStrictly\":false,\"params\":{},\"roleKey\":\"address_book_admin\",\"roleName\":\"通讯录管理员-app\"},{\"admin\":false,\"createTime\":\"2023-09-28 10:09:28\",\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"flag\":false,\"menuCheckStrictly\":false,\"params\":{},\"roleKey\":\"workClothes-manager\",\"roleName\":\"工作服管理员-app\"},{\"admin\":false,\"createTime\":\"2023-10-08 10:40:27\",\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"flag\":false,\"menuCheckStrictly\":false,\"params\":{},\"roleKey\":\"calendar-manager-app\",\"roleName\":\"日程管理员-app\"}],\"sex\":\"1\",\"status\":\"0\",\"userId\":\"582658115703843401\",\"userLoginName\":\"xiaowenyaoshanghai\",\"userName\":\"肖文瑶上海\"}",
				"permissions": "[\"energymanger:energyCompare:contractEdit\",\"energymanger:gasEnergy:dailyGas\",\"energymanger:energyPrice:electricAdd\",\"partyBuild:meetingcaurse:partyClass:add\",\"partybuild:notification:edit\",\"energymanger:benchmarkData:electricRemove\",\"energymanger:steamEnergy:monthlyAdd\",\"partyBoard:themeActivity:edit\",\"partybuild:news:list\",\"energymanger:benchmarkData:gasAdd\",\"energymanger:gasEnergy:periodEdit\",\"energymanger:energyPrice:gasList\",\"pbserver:schedule:view\",\"energymanger:stationManagement:energytype:edit\",\"partybuild:learningcolumn:remove\",\"energymanger:gasEnergy:monthlyEdit\",\"energymanger:equipment:remove\",\"partyBuild:meetingcaurse:partyMeeting:remove\",\"energymanger:benchmarkData:gasEdit\",\"address:book:getDeptUsers\",\"partybuild:learningcolumn:edit\",\"partybuild:learningcolumn:commentPublic\",\"problemLibrary:stationList:edit\",\"partybuild:application:list\",\"energymanger:stationManagement:energyType\",\"training:notice:list\",\"energymanger:equipment:hostAdjustment:remove\",\"energymanger:equipment:hostAdjustment:details:remove\",\"partybuild:publicsummons:remove\",\"energymanger:stationManagement:consume\",\"partybuild:development:remove\",\"energymanger:stationManagement:consume:add\",\"partybuild:publicsummons:edit\",\"energymanger:gasEnergy:dailyAdd\",\"communicate:removeArticle\",\"partybuild:homepagebanner:add\",\"energymanger:energyCompare:contractConsumption\",\"energymanger:gasEnergy:period\",\"partybuild:department:edit\",\"energymanger:equipment:endAdjustment:edit\",\"partyBuild:meetingcaurse:notification:list\",\"energymanger:energyCompare:budgetConsumption\",\"partyBoard:politicalStudy:list\",\"system:dict:add\",\"partyBuild:meetingcaurse:partyMeeting:list\",\"energymanger:steamEnergy:monthlyRemove\",\"partybuild:modelposition:preview\",\"energymanger:energyPrice:steamEdit\",\"partybuild:modelposition:list\",\"energymanger:stationManagement:add\",\"energymanger:stationManagement:consume:remove\",\"partyBuild:meetingcaurse:partyMeeting:add\",\"communicate:articleDetail\",\"energymanger:electricEnergy:monthlyRemove\",\"partyBoard:themeActivity:remove\",\"projectManage:projectInfo:remove\",\"energymanger:equipment:nextDayPlan:copy\",\"partyBuild:meetingcaurse:partyClass:edit\",\"partyBuild:meetingcaurse:auditCenter:audit\",\"energymanger:electricEnergy:yearlyEdit\",\"communicate:articleReply\",\"energymanger:electricEnergy:dailyAdd\",\"partyBoard:themeActivity:add\",\"partyBoard:themeActivity:list\",\"energymanger:steamEnergy:dailyRemove\",\"energymanger:electricEnergy:dailyImprot\",\"energymanger:gasEnergy:monthlyList\",\"partybuild:meetingcaurse:report:list\",\"partybuild:homepagebanner:edit\",\"address:book:queryUsersByDeptId\",\"energymanger:energyPrice:electricRemove\",\"partyBuild:meetingcaurse:notification:add\",\"partyBoard:personInfo:remove\",\"energymanger:stationManagement:energytype:add\",\"system:dict:list\",\"energymanger:monthlyElectricity:add\",\"energymanger:benchmarkData:steamList\",\"energymanger:benchmarkData:gasList\",\"energymanger:equipment:nextDayPlan\",\"energymanger:gasEnergy:periodRemove\",\"energymanger:steamEnergy:dailySteam\",\"energymanger:energyCompare:settlementList\",\"problemLibrary:projectList:add\",\"partybuild:visitcard:list\",\"partybuild:visitcard:add\",\"problemLibrary:projectList:list\",\"energymanger:gasEnergy:monthlyGas\",\"energymanger:equipment:edit\",\"partybuild:publicsummons:list\",\"energymanger:electricEnergy:monthlyList\",\"energymanger:steamEnergy:monthlyEdit\",\"partyBoard:politicalStudy:remove\",\"partyBuild:meetingcaurse:auditCenter:list\",\"partybuild:themeactivity:list\",\"address:book:getUserCompleteInfo\",\"energymanger:steamEnergy:period\",\"problemLibrary:stationList:list\",\"partybuild:department:list\",\"partyBuild:meetingcaurse:partyClass:list\",\"energymanger:electricEnergy:yearlyList\",\"energymanger:stationManagement:inventory:add\",\"pbadmin:schedule:list:add\",\"problemLibrary:questionList:export\",\"energymanger:energyCompare:budgetList\",\"partybuild:news:remove\",\"problemLibrary:projectList:remove\",\"energymanger:energyPrice:steamRemove\",\"energymanger:steamEnergy:dailyList\",\"energymanger:energyPrice:steamAdd\",\"energymanger:equipment:endAdjustment:remove\",\"partybuild:development:edit\",\"partybuild:department:add\",\"energymanger:electricEnergy:dailyList\",\"energymanger:powerRoom:list\",\"partybuild:news:query\",\"energymanger:stationManagement:inventory\",\"energymanger:stationManagement:inventory:remove\",\"partybuild:application:remove\",\"pbadmin:schedule:list:edit\",\"pbserver:schedule:add\",\"energymanger:equipment:nextDayPlan:edit\",\"partyBoard:politicalStudy:edit\",\"problemLibrary:questionList:remove\",\"partybuild:probationary:list\",\"energymanger:equipment:hostAdjustment:copy\",\"problemLibrary:questionList:edit\",\"energymanger:steamEnergy:monthlySteam\",\"partybuild:partymember:list\",\"energymanger:powerRoom:copy\",\"workClothes:view\",\"system:dict:edit\",\"energymanger:benchmarkData:steamAdd\",\"partybuild:themeactivity:add\",\"energymanger:monthlyElectricity:remove\",\"energymanger:energyPrice:electricEdit\",\"partybuild:themeactivity:edit\",\"energymanger:steamEnergy:periodAdd\",\"problemLibrary:stationList:remove\",\"partyBuild:meetingcaurse:notification:edit\",\"system:dict:query\",\"energymanger:gasEnergy:dailyList\",\"energymanger:stationManagement:energytype:remove\",\"energymanger:equipment:nextDayPlan:remove\",\"partybuild:partymember:remove\",\"partybuild:homepagebanner:remove\",\"energymanger:stationManagement:edit\",\"energymanger:electricEnergy:monthlyElectricity\",\"energymanger:benchmarkData:steamEdit\",\"energymanger:steamEnergy:dailyAdd\",\"problemLibrary:projectList:edit\",\"energymanger:energyCompare:settlementEdit\",\"partybuild:notification:remove\",\"problemLibrary:stationList:add\",\"energymanger:steamEnergy:dailyEdit\",\"energymanger:gasEnergy:dailyRemove\",\"energymanger:energyPrice:gasAdd\",\"energymanger:energyPrice:gasRemove\",\"energymanger:equipment:list\",\"communicate:articleList\",\"energymanger:stationManagement:consume:edit\",\"partybuild:development:add\",\"system:dict:remove\",\"energymanger:stationManagement:inventory:edit\",\"energymanger:benchmarkData:electricList\",\"energymanger:energyCompare:budgetEdit\",\"partybuild:development:list\",\"problemLibrary:questionList:add\",\"energymanger:monthlyElectricity:list\",\"pbserver:schedule:me\",\"energymanger:gasEnergy:monthlyAdd\",\"energymanger:equipment:hostAdjustment:add\",\"partybuild:visitcard:edit\",\"partybuild:meetingcaurse:report:add\",\"energymanger:gasEnergy:monthlyRemove\",\"energymanger:energyCompare:settlementConsumption\",\"projectManage:projectInfo:edit\",\"energymanger:steamEnergy:periodList\",\"energymanger:equipment:endAdjustment\",\"energymanger:steamEnergy:periodRemove\",\"energymanger:equipment:hostAdjustment:details:edit\",\"partybuild:themeactivity:remove\",\"pbadmin:schedule:list:remove\",\"energymanger:powerRoom:remove\",\"energymanger:electricEnergy:yearlyElectricity\",\"projectManage:projectInfo:detail\",\"partybuild:homepagebanner:list\",\"partybuild:visitcard:remove\",\"partybuild:meetingcaurse:report:remove\",\"energymanger:gasEnergy:periodList\",\"energymanger:electricEnergy:dailyEdit\",\"energymanger:energyPrice:gasEdit\",\"partybuild:learningcolumn:list\",\"partybuild:modelposition:export\",\"energymanger:equipment:hostAdjustment:edit\",\"partyBoard:personInfo:import\",\"energymanger:powerRoom:add\",\"partyBoard:politicalStudy:add\",\"partybuild:notification:list\",\"partybuild:publicsummons:add\",\"problemLibrary:questionList:list\",\"energymanger:powerRoom:edit\",\"partybuild:probationary:edit\",\"training:notice:remove\",\"training:notice:edit\",\"energymanger:equipment:add\",\"energymanger:gasEnergy:dailyEdit\",\"energymanger:equipment:endAdjustment:copy\",\"communicate:comment\",\"partybuild:application:edit\",\"energymanger:stationManagement:list\",\"partybuild:department:remove\",\"energymanger:equipment:hostAdjustment:details\",\"energymanger:equipment:hostAdjustment\",\"partybuild:news:edit\",\"energymanger:energyCompare:contractList\",\"energymanger:benchmarkData:electricAdd\",\"partyBuild:meetingcaurse:notification:remove\",\"energymanger:benchmarkData:gasRemove\",\"partybuild:news:add\",\"energymanger:monthlyElectricity:edit\",\"partyBuild:meetingcaurse:auditCenter:edit\",\"partyBuild:meetingcaurse:partyMeeting:edit\",\"partybuild:notification:add\",\"energymanger:energyPrice:steamList\",\"energymanger:electricEnergy:monthlyEdit\",\"projectSchedule:view\",\"energymanger:gasEnergy:periodAdd\",\"address:book:deptTreeList\",\"projectManage:projectInfo:list\",\"energymanger:electricEnergy:monthlyAdd\",\"energymanger:benchmarkData:steamRemove\",\"energymanger:steamEnergy:monthlyList\",\"energymanger:stationManagement:remove\",\"energymanger:equipment:endAdjustment:add\",\"training:notice:add\",\"communicate:removeComment\",\"partybuild:partymember:add\",\"partybuild:learningcolumn:add\",\"energymanger:electricEnergy:dailyElectricity\",\"partybuild:meetingcaurse:report:edit\",\"energymanger:equipment:nextDayPlan:add\",\"partybuild:probationary:add\",\"energymanger:electricEnergy:dailyRemove\",\"energymanger:steamEnergy:periodEdit\",\"pbadmin:schedule:list:query\",\"partybuild:probationary:remove\",\"partybuild:application:add\",\"energymanger:benchmarkData:electricEdit\",\"energymanger:equipment:hostAdjustment:details:add\",\"projectManage:projectInfo:add\",\"energymanger:energyPrice:electricList\",\"energymanger:electricEnergy:yearlyRemove\",\"partybuild:partymember:edit\",\"energymanger:electricEnergy:yearlyAdd\",\"partyBuild:meetingcaurse:partyClass:remove\"]",
				"roles": "[\"dept-manager\",\"address_book_admin\",\"train_certificate_employee\",\"pb_admin\",\"calendar-manager-app\",\"media_manage\",\"workClothes-manager\",\"pbserver_itinerary_manage\"]",
				"environment": "pntest",
				"darkmode": "light"
			}
			console.log(obj);

			let token = obj.token;
			let environment = obj.environment;
			console.log("App Show  environment ", environment);

			let userInfo = obj.userInfo;
			let permissions = obj.permissions;
			let roles = obj.roles;
			if (token) {
				storage.setStorageDataSync("token", token);
			}
			if (environment) {
				storage.setStorageDataSync("environment", environment);
			}
			if (userInfo) {
				if (typeof(userInfo) == 'string') {
					let userInfoObj = JSON.parse(userInfo)
					console.log("App Show userInfoObj ====" + JSON.stringify(userInfoObj));
					storage.setStorageDataSync("userInfo", userInfoObj);
				} else {
					storage.setStorageDataSync("userInfo", userInfo);
				}
			}
			if (permissions) {
				if (typeof(permissions) == 'string') {
					let permissionsObj = JSON.parse(permissions)
					console.log("App Show permissionsObj ====" + JSON.stringify(permissionsObj));
					storage.setStorageDataSync("permissions", permissionsObj);
				} else {
					storage.setStorageDataSync("permissions", permissions);
				}

			}
			if (roles) {
				if (typeof(roles) == 'string') {
					let rolesObj = JSON.parse(roles)
					console.log("App Show rolesObj ====" + JSON.stringify(rolesObj));
					storage.setStorageDataSync("roles", rolesObj);
				} else {
					storage.setStorageDataSync("roles", roles);
				}
			}
			var that = this
			let system = uni.getSystemInfoSync().platform
			// 获取本地应用资源版本号
			plus.runtime.getProperty(plus.runtime.appid, (inf) => {
				console.log("获取本地应用资源版本号======", inf);
				//获取服务器的版本号
				apis.checkUpgradeInfo({
						pageNum: "1",
						pageSize: "1",
						moduleId: inf.appid,
						packageType: "1",
						sysType: system == 'android' ? '2' : '3',
						editionIssue: "1"
					})
					.then(res => {
						let result = res.res.data.data
						//res.data.xxx根据后台返回的数据决定（我这里后端返回的是data），所以是res.data.data
						//判断后台返回版本号是否大于当前应用版本号 && 是否发行 （上架应用市场时一定不能弹出更新提示）
						if (Number(result.editionName) > Number(inf.versionCode) && result
							.editionIssue == 1 && result.packageType == 1) {
							//如果是wgt升级，并且是静默更新 （注意！！！ 如果是手动检查新版本，就不用判断静默更新，请直接跳转更新页，不然点击检查新版本后会没反应）
							if (result.editionSilence == "1") {
								//调用静默更新方法 传入下载地址
								silenceUpdate(result.editionUrl)
							} else {
								//跳转更新页面 （注意！！！如果pages.json第一页的代码里有一打开就跳转其他页面的操作，下面这行代码最好写在setTimeout里面设置延时3到5秒再执行）
								setTimeout(() => {
									uni.navigateTo({
										url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
											encodeURIComponent(JSON.stringify(result
												.editionUrl))
									});
								}, 1000)
							}
						} else {
							console.log("已是最新版本");
						}
					})
					.catch(reject => {});
			});
			//#endif  

			//#ifndef APP-PLUS
			storage.setStorageDataSync("token", "");
			storage.setStorageDataSync("environment", "test");
			//#endif  
		}
	};
</script>

<style lang="scss">
	@import 'uview-ui/index.scss';
	@import './common/styles/globalStyle.css';
	@import './common/styles/fontStyle.css';
</style>